package com.animal.controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.animal.dao.DailyManage;

public class DailyServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public DailyServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	@SuppressWarnings("null")
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		String dos = request.getParameter("do");

		if (dos == null || dos.equals("")) {
			
			//out.print("<script>window.location='ExperimentalFileManagement.jsp';</script>");
			
			response.sendRedirect("DailyFileManagement.jsp");
			
		}
		if (dos.equals("archive")) {
			
			String id = request.getParameter("id");
			String FileState="已经审核";
			
			boolean b = DailyManage.updateState(id, FileState);
			
			if(b){
				out.print("<script>alert('审核成功!');window.location='DailyFileManagement.jsp';</script>");
			}
			else{
				out.print("<script>alert('审核失败!');</script>");
			}
			return;
		}
		
		else if(dos.equals("del")){
			String id = request.getParameter("id");
			
			boolean b = DailyManage.delete(id);
			
			if(b){
				out.print("<script>alert('删除成功!');window.location='DailyFileManagement.jsp';</script>");
				
			}else{
				out.print("<script>alert('删除失败!');</script>");
			}
			return;
		}
		
		else if(dos.equals("update")){
		 	String id = request.getParameter("id");
		 	String user = request.getParameter("user");
	        String ft = request.getParameter("filetitle");
	        String ai = request.getParameter("animalid");
	        String ddt = request.getParameter("ddt");

	        request.getSession().setAttribute("filenumber", id);
	        request.getSession().setAttribute("user", user);
	        request.getSession().setAttribute("ft", ft);
	        request.getSession().setAttribute("ai", ai);
	        request.getSession().setAttribute("ddt", ddt);

	        request.getRequestDispatcher("DailyFileUpdate.jsp").forward(request, response);
	        //response.sendRedirect("DailyFileManagement.jsp");
			
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException { 
		// Put your code here
	}

}